package com.songlanyun.modules.message.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.enums.SysConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.custom.ClassConvertUtils;
import com.songlanyun.modules.message.model.*;
import com.songlanyun.modules.message.model.to.PrivateMessageTO;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * app站内消息
 *
 * @author wuchao
 * @email sunlightcs@gmail.com
 * @date 2021-03-04 16:05:09
 */
@Data
@TableName("yxt_private_message")
public class PrivateMessage extends RootEntity<Long, PrivateMessage> implements Serializable {
    private static final long serialVersionUID = 1L;

    public PrivateMessage() {
    }

    public PrivateMessage(PrivateMessageTO to) {
        this.userId = to.getUserId();
        this.type = to.getType();
        this.msg = ClassConvertUtils.transformJSONToSnake(to.getMessageBody());
        this.remark = to.getRemark();
    }

    @TableId
    private Long id;

    /**
     * 会员ID
     */
    @NotNull(message = "会员不能为空")
    private Long userId;

    /**
     * 消息类型
     * 0：活动消息
     * 1：支付消息
     * 2：物流消息
     * 3：售后消息
     * 4：互动消息
     */
    @NotNull(message = "消息类型不能为空")
    private PrivateMessageConstant.MessageType type;


    /**
     * 消息内容
     */
    private String msg;

    /**
     * 消息体
     */
    @TableField(exist = false)
    private IMessageBody messageBody;

    @SuppressWarnings("all")
    public IMessageBody loadMessageBody() {
        try {
            switch (type) {
                case ACTIVITY_MESSAGE:
                    this.messageBody = ClassConvertUtils.stringToClassSnake(this.msg, new TypeReference<ActivityMessage>() {
                    });
                    break;
                case PAY_MESSAGE:
                    this.messageBody = ClassConvertUtils.stringToClassSnake(this.msg, new TypeReference<PayMessage>() {
                    });
                    break;
                case LOGISTICS_MESSAGE:
                    this.messageBody = ClassConvertUtils.stringToClassSnake(this.msg, new TypeReference<LogisticsMessage>() {
                    });
                    break;
                case RIGHTS_MESSAGE:
                    this.messageBody = ClassConvertUtils.stringToClassSnake(this.msg, new TypeReference<RightsMessage>() {
                    });
                    break;
                case INTERACTION_MESSAGE:
                    this.messageBody = ClassConvertUtils.stringToClassSnake(this.msg, new TypeReference<InteractionMessage>() {
                    });
                    break;
                case SYSTEM_MESSAGES:
                    this.messageBody = ClassConvertUtils.stringToClassSnake(this.msg, new TypeReference<SystemMessage>() {
                    });
                    break;
                default:
                    return null;
            }
            return this.messageBody;
        } catch (Exception e) {
            throw new RRException("消息内容转换失败");
        }
    }

    /**
     * 消息推送时，设置消息体
     */
    public PrivateMessage pushMessageBody() {
        this.loadMessageBody();
        this.setMsg(null);
        return this;
    }

    /**
     * 消息备注
     */
    @NotBlank(message = "消息备注不能为空")
    private String remark;

    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 是否已读
     */
    private Boolean isRead;


    @Override
    public Long getIdentity() {
        return this.getId();
    }


    public static void main(String[] args) throws IOException {
        String msg = "{\"goods_type\":1,\"activity_type\":null,\"goods_num\":1,\"money\":1002.00,\"goods\":[{\"goods_id\":3,\"goods_name\":\"美体\",\"goods_img\":\"http://www.baidu.com/1.jpg\",\"goods_spec\":\"\"}]}";
        PayMessage payMessage = ClassConvertUtils.stringToClassSnake(msg, new TypeReference<PayMessage>() {
        });
        System.out.println(payMessage);

    }
}
